/**
 * 
 */
package com.audi.box.server.handler;

import android.util.Log;

import com.audi.box.controller.IController;
import com.audi.box.controller.SmartTriggerController;
import com.audi.box.server.message.HandlerException;
import com.audi.dto.SmartTriggerTriggerItem;
import com.audi.dto.SmartTriggerWhenItem;
import com.audi.dto.SmartTriggerWhenTriggerItem;

/**
 * @author HuyPVA
 * 
 */
public class SmartTriggerHandler extends Handler {
	private static SmartTriggerHandler stHandler;
	private static Object stObj = new Object();

	/**
	 * @param controller
	 */
	private SmartTriggerHandler(IController controller) {
		super(controller);
	}

	public static SmartTriggerHandler getInstance(IController controller) {
		if (stHandler == null) {
			synchronized (stObj) {
				if (stHandler == null) {
					stHandler = new SmartTriggerHandler(controller);
				}
				return stHandler;
			}
		} else {
			return stHandler;
		}
	}

	/*
	 * Get DeviceController
	 */
	public SmartTriggerController getSmartTriggerController() {
		return (SmartTriggerController) getController();
	}

	/*
	 * Process message by action
	 */
	@Override
	public Object process(int actionId, String data) throws HandlerException {
		Object result = null;
		SmartTriggerWhenItem stWhenItem = null;
		SmartTriggerTriggerItem stTriggerItem = null;
		SmartTriggerWhenTriggerItem stWhenTriggerItem = null;

		switch (actionId) {

		case ADD_WHEN_ST:
			stWhenItem = getGson().fromJson(data, SmartTriggerWhenItem.class);
			result = getSmartTriggerController().addSTWhen(stWhenItem);
			break;
		case ADD_TRIGGER_ST:
			stTriggerItem = getGson().fromJson(data,
					SmartTriggerTriggerItem.class);
			result = getSmartTriggerController().addSTTrigger(stTriggerItem);
			break;
		case ADD_WHEN_TRIGGER_ST:
			stWhenTriggerItem = getGson().fromJson(data,
					SmartTriggerWhenTriggerItem.class);
			result = getSmartTriggerController().addSTWhenTrigger(
					stWhenTriggerItem);
			break;
		case UPDATE_WHEN_ST:
			stWhenItem = getGson().fromJson(data, SmartTriggerWhenItem.class);
			result = getSmartTriggerController().updateSTWhen(stWhenItem);
			break;
		case UPDATE_TRIGGER_ST:
			stTriggerItem = getGson().fromJson(data,
					SmartTriggerTriggerItem.class);
			result = getSmartTriggerController().updateSTTrigger(stTriggerItem);
			break;
		case UPDATE_WHEN_TRIGGER_ST:
			stWhenTriggerItem = getGson().fromJson(data,
					SmartTriggerWhenTriggerItem.class);
			result = getSmartTriggerController().updateSTWhenTrigger(
					stWhenTriggerItem);
			break;
		case DELETE_WHEN_ST:
			stWhenItem = getGson().fromJson(data, SmartTriggerWhenItem.class);
			result = getSmartTriggerController().deleteSTWhen(stWhenItem);
			break;
		case DELETE_TRIGGER_ST:
			stTriggerItem = getGson().fromJson(data,
					SmartTriggerTriggerItem.class);
			result = getSmartTriggerController().deleteSTTrigger(stTriggerItem);
			break;
		case DELETE_WHEN_TRIGGER_ST:
			stWhenTriggerItem = getGson().fromJson(data,
					SmartTriggerWhenTriggerItem.class);
			result = getSmartTriggerController().deleteSTWhenTrigger(
					stWhenTriggerItem);
			break;
		case GET_WHEN_ST_BY_ID:
			int whenId = getGson().fromJson(data, Integer.class);
			result = getSmartTriggerController().getSTWhen(whenId);
			break;
		case GET_TRIGGER_ST_BY_ID:
			int triggerId = getGson().fromJson(data, Integer.class);
			result = getSmartTriggerController().getSTTrigger(triggerId);
			break;
		case GET_WHEN_TRIGGER_ST_BY_ID:
			int whenTriggerId = getGson().fromJson(data, Integer.class);
			result = getSmartTriggerController()
					.getSTWhenTrigger(whenTriggerId);
			break;
		case GET_ALL_ST:
			result = getSmartTriggerController().getSmartTriggerFullList();
			break;
		case GET_LIST_WHEN_ST:
			result = getSmartTriggerController().getSTWhenList();
			break;
		case GET_LIST_TRIGGER_ST:
			result = getSmartTriggerController().getSTTriggerList();
			break;
		case GET_LIST_WHEN_TRIGGER_ST:
			result = getSmartTriggerController().getSTWhenTriggerList();
			break;
		case SET_ON_OFF_ST:
			stWhenTriggerItem = getGson().fromJson(data,
					SmartTriggerWhenTriggerItem.class);
			result = getSmartTriggerController().setOnOffSmartTrigger(
					stWhenTriggerItem);
			break;
		case GET_DEVICE_FROM_WHEN_ST:
			stWhenItem = getGson().fromJson(data, SmartTriggerWhenItem.class);
			result = getSmartTriggerController().getDeviceValueFromSTWhen(
					stWhenItem);
			break;
		case GET_DEVICE_FROM_TRIGGER_ST:
			stTriggerItem = getGson().fromJson(data,
					SmartTriggerTriggerItem.class);
			result = getSmartTriggerController().getDeviceValueFromSTTrigger(
					stTriggerItem);
			break;
		default:
			Log.e("AudiBox",
					"[SmartTriggerHandler.java] Function not support: "
							+ actionId);
			throw new HandlerException("Function " + actionId +" not found!!!");
		}
		return result;

	}

}
